package com.shijie.controller.app;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Magazines;
import com.shijie.domain.entity.TalentTrials;
import com.shijie.domain.entity.Users;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.domain.enums.EnumInterface.EntityType;
import com.shijie.domain.enums.EnumInterface.Status;
import com.shijie.service.MagazinesService;
import com.shijie.service.TalentTrialsService;
import com.shijie.service.TalentsService;
import com.shijie.service.UserAttentionsService;

/*
 *  达人相关接口
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/talent")
public class AppTalentController {

	@Resource
	private TalentsService talentsService;

	@Resource
	private AppUserService appUserService;
	
	@Resource
	private MagazinesService magazinesService;
	
	@Resource
	private UserAttentionsService userAttentionsService;
	
	@Resource
	private TalentTrialsService talentTrialsService;

	@GetMapping
	public Result list(@RequestParam(defaultValue = "0") Integer page,
			@RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) String q) {
		Users user = new Users();
		user.setUserType(EnumInterface.UserType.TALENT.ordinal());
		user.setQ(q);
		return ResultGenerator.success(talentsService.selectTalentsByPage(page, pageSize, user));

	}

	@GetMapping(value = "/{talentId}")
	public Result detail(@PathVariable Integer talentId) {
		Users user = talentsService.getTalentsDetail(talentId);
		// 判断是否关注达人
		user.setIsAttentionOwner(userAttentionsService.isAttentionTalentOwner(appUserService.getCurrentUser().getId(),
				talentId, EnumInterface.Status.VALID.ordinal()));
		return ResultGenerator.success(user);
	}

	/**
	 * 达人自己的文章
	 * 
	 * @param talentId
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value = "/{talentId}/@magazines")
	public Result listMagazines(@PathVariable Integer talentId, @RequestParam(defaultValue = "0") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Magazines> list = magazinesService.getMagazinesListByType(EntityType.TALENT_ARTICLE, talentId,
				Status.VALID.ordinal(), null);
		PageInfo<Magazines> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}
	
	/**
	 * 达人自己的关注达人列表
	 * 
	 * @param talentId
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value = "/{talentId}/@attentions")
	public Result listAttentions(@PathVariable Integer talentId, @RequestParam(defaultValue = "0") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Users> list = talentsService.getAttentionsTalentById(talentId);
		PageInfo<Users> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}

	/**
	 * 推荐达人列表数据
	 *
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping("recommends")
	public Result recommends(@RequestParam(defaultValue = "0") Integer page,
							 @RequestParam(defaultValue = "5") Integer pageSize) {
		return ResultGenerator.success(talentsService.recommendTalents(appUserService.getCurrentUser().getId(), page, pageSize));

	}
	
	
	/**
	 * 推荐使用列表
	 *
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/{talentId}/@trials")
	public Result trials(@PathVariable Integer talentId,@RequestParam(defaultValue = "1") Integer page,
							 @RequestParam(defaultValue = "5") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
        Condition condition = new Condition(TalentTrials.class);
        Example.Criteria c = condition.createCriteria();
        c.andEqualTo("talentId", talentId);
        List<TalentTrials> list = talentTrialsService.selectByCondition(condition);
        PageInfo<TalentTrials> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);

	}
	
}

